﻿
<?php

/**
    建筑刷新，完成需要完成的建筑，拆除需要拆除的建筑，并更新其他一些数据
 *   @author pigsunny
 *   @copyright 2012
*/
 

 require_once("./common.php");
 require_once("./utils.php");
 require_once("./utilsExtend.php");

class BuildingCron{

	function updateBuilding($param)
	{
		
		$rows = sql_fetch_rows("select * from mem_building_upgrading where state_endtime < unix_timestamp()");
        //取出已经达到完成时间的升级建筑队列
		foreach($rows as $row) {
			sql_query("update sys_building set level =".$row["level"].", state = 0 where id = ".$row["id"]);
            $uid=sql_fetch_one_cell("SELECT uid FROM sys_city WHERE cid=".$row["cid"]);
            //更改建筑等级、状态
            switch ($row["bid"]){
            case 1: {
                //建筑是农田，则更新资源、储量、任务
                sql_query("update mem_city_resource set food_add=food_add+".$row["level"]."*10000,
                food_max=food_max+".$row["level"]."*1000000,
                people_working=people_working+".$row["level"]."*1 where cid=".$row["cid"]);
                completeTask($uid,15);
                $food_add=sql_fetch_one_cell("SELECT food_add FROM mem_city_resource WHERE cid=".$row["cid"]);
                if ($food_add>=100000){
                    completeTask($uid,189);
                }
                if ($food_add>=500000){
                    completeTask($uid,190);
                }
                if ($food_add>=1000000){
                    completeTask($uid,191);
                }
                if ($food_add>=5000000){
                    completeTask($uid,192);
                }
                if ($row["level"]>=5){
                    completeTask($uid,165);
                }
                break;} 
            
            case 2:{
                //建筑是伐木场，则更新资源、储量、任务
                sql_query("update mem_city_resource set wood_add=wood_add+".$row["level"]."*10000,
                wood_max=wood_max+".$row["level"]."*1000000,
                people_working=people_working+".$row["level"]."*10 where cid=".$row["cid"]);
                completeTask($uid,16);
                $wood_add=sql_fetch_one_cell("SELECT wood_add FROM mem_city_resource WHERE cid=".$row["cid"]);
                if ($wood_add>=100000){
                    completeTask($uid,193);
                }
                if ($wood_add>=500000){
                    completeTask($uid,194);
                }
                if ($wood_add>=1000000){
                    completeTask($uid,195);
                }
                if ($wood_add>=5000000){
                    completeTask($uid,196);
                }
                if ($row["level"]>=2){
                    completeTask($uid,163);
                }
                break;}
            
            case 3:{
                //建筑是石料，则更新资源、储量、任务
                sql_query("update mem_city_resource set rock_add=rock_add+".$row["level"]."*10000,
                rock_max=rock_max+".$row["level"]."*1000000,
                people_working=people_working+".$row["level"]."*20 where cid=".$row["cid"]);
                completeTask($uid,17);
                $rock_add=sql_fetch_one_cell("SELECT rock_add FROM mem_city_resource WHERE cid=".$row["cid"]);
                if ($rock_add>=100000){
                    completeTask($uid,197);
                }
                if ($rock_add>=500000){
                    completeTask($uid,198);
                }
                if ($rock_add>=1000000){
                    completeTask($uid,199);
                }
                if ($rock_add>=5000000){
                    completeTask($uid,200);
                }
                if ($row["level"]>=2){
                    completeTask($uid,137);
                }
                break;}
                
            case 4:{
                //建筑是铁矿，则更新资源、储量、任务
                sql_query("update mem_city_resource set iron_add=iron_add+".$row["level"]."*10000,
                iron_max=iron_max+".$row["level"]."*1000000,
                people_working=people_working+".$row["level"]."*25 where cid=".$row["cid"]);
                completeTask($uid,18);
                $iron_add=sql_fetch_one_cell("SELECT iron_add FROM mem_city_resource WHERE cid=".$row["cid"]);
                if ($iron_add>=100000){
                    completeTask($uid,201);
                }
                if ($iron_add>=500000){
                    completeTask($uid,202);
                }
                if ($iron_add>=1000000){
                    completeTask($uid,203);
                }
                if ($iron_add>=5000000){
                    completeTask($uid,204);
                }
                break;}
                
            case 5:{
                //民房
                updateCityPeopleMax($row["cid"]);
                completeTask($uid,1);
                if ($row["level"]>=2){
                    completeTask($uid,2);
                }
                $people_max = sql_fetch_one_cell("select sum(level*(level*10)*5000) from sys_building where `cid`=".$row["cid"]." and bid=".ID_BUILDING_HOUSE);
                if ($people_max>=100000){
                    completeTask($uid,3);
                }
                if ($people_max>=1000000){
                    completeTask($uid,4);
                }
                if ($people_max>=5000000){
                    completeTask($uid,5);
                }
                break;}
            case 6:{
                //官府
                sql_query("update mem_city_resource set gold_max=gold_max+".$row["level"]."*100000000 where cid=".$row["cid"]);
                completeTask($uid,$row["level"]+25);
                if ($row["level"]>=2){
                    completeTask($uid,224);
                }
                if ($row["level"]>=4){
                    completeTask($uid,226);
                }
                if ($row["level"]>=5){
                    completeTask($uid,363);
                    sql_query("UPDATE sys_user SET state=0 WHERE uid=".$uid);
                }
                if ($row["level"]>=6){
                    completeTask($uid,229);
                }
                if ($row["level"]>=8){
                    completeTask($uid,232);
                }
                if ($row["level"]>=10){
                    completeTask($uid,235);
                }
                break;}
            case 7:{
                //学院
                completeTask($uid,$row["level"]+35);
                updateUserTechnic($uid);
                break;}
            case 8:{
                //校场
                completeTask($uid,90);
                break;}
            case 9:{
                //军营
                completeTask($uid,$row["level"]+90);
                break;}
            case 10:{
                //客栈
                completeTask($uid,82);
                break;}
            case 11:{
                //招贤馆
                completeTask($uid,83);
                break;}
            case 12:{
                //鸿胪寺
                completeTask($uid,66);
                break;}
            case 13:{
                //市场
                completeTask($uid,22);
                break;}
            case 14:{
                //铁匠铺
                completeTask($uid,134);
                if ($row["level"]>=2){ 
                    completeTask($uid,135);
                }
                break;}
            case 15:{
                //工匠作坊
                completeTask($uid,136);
                break;}
            case 16:{
                //马厩
                completeTask($uid,166);
                break;}
            case 17:{
                //仓库
                completeTask($uid,164);
                break;}
            case 18:{
                //驿站
                completeTask($uid,167);
                break;}
            case 19:{
                //烽火台
                completeTask($uid,$row["level"]+169);
                break;}
            case 20:{
                //城墙
                completeTask($uid,$row["level"]+137);
                break;}
            default:{
                
            }
            }
			sql_query("DELETE FROM mem_building_upgrading WHERE id=".$row["id"]);
		
        }


		return true;

	}
    
    function destroyBuilding($param){
        
    }
}
 ?>
